﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Data;
using MyAlbum.Utility;
using MyAlbum.Logging;

namespace MyAlbum.Services
{
    public class SectionService : ISectionService
    {
        private ISectionRepository _repository;
        private IUser _user;
        private ILogger _logger;
        public SectionService(ISectionRepository sectionRepository,
            IUser user,
            ILogger logger)
        {
            Assert.IsNotNull(sectionRepository);
            this._repository = sectionRepository;
            this._user = user;
            this._logger = logger;
        }
        public int CountItems()
        {
            return this._repository.CountItems(true);
        }
        public List<Section> GetItems()
        {
            bool visible = _user == null || _user.ShowOnlyVisibleEntries();
            return this._repository.GetItems(visible);
        }


        public void DeleteItem(Section item)
        {
            if (item != null)
            {
                if (_user.AllowActions())
                {
                    this._repository.DeleteItem(item);
                }
                else
                {
                    _logger.Warning("An unauthorized attempt to delete section (" + item.Id + ") by " + (_user == null ? " null user " : _user.Name));
                }
            }
            else
            {
                _logger.Warning("Attempt to delete null section");
            }
        }

        public void UpdateItem(Section section, IValidationSummary summary)
        {
            if (_user.AllowAdd())
            {
                Assert.IsNotNull(section);
                if (!FormsValidation.ContainsCharacters(section.Name))
                {
                    summary.AddError("Name", "Missing");
                }
                if (summary.IsValid)
                {
                    this._repository.UpdateItem(section);
                }
            }
            else
            {
                summary.AddError("Album", "Unauthorized");
                _logger.Warning("An unauthorized attempt to edit album (" + section.Id + ") by " + (_user == null ? " null user " : _user.Name));
            }
        }

        #region ISectionService Members

        public Section GetItemById(string sectionId)
        {
            var section = this._repository.GetItemById(sectionId);
            if (section != null &&
                !section.Visible &&
                _user.ShowOnlyVisibleEntries())
            {
                _logger.Warning("An attempt to view non visible section (" + sectionId + ") by " + (_user == null ? " null user " : _user.Name));
                section = null;
            }
            return section;
        }


        public void AddItem(Section item, IValidationSummary summary)
        {
            Assert.IsNotNull(item);
            if (_user.AllowAdd())
            {
                if (!FormsValidation.ContainsCharacters(item.Name))
                {
                    summary.AddError("Name", "Missing");
                }
                if (summary.IsValid)
                {
                    item.UserName = _user.Name;
                    item.TimeStamp = DateTime.UtcNow;
                    this._repository.AddItem(item);
                }
            }
            else
            {
                summary.AddError("Section", "Unauthorized");
                _logger.Warning("An unauthorized attempt to add section  by " + (_user == null ? " null user " : _user.Name));
            }
        }

        #endregion
    }
}
